/**
 * @file win_uart.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2024-01-30
 * 
 * @copyright Copyright (c) 2024
 * 
 */
#include "win_uart.h"
#include <stdio.h>
#include <conio.h>
#include "stdint.h"
#include "time.h"

/**
 * @brief 字符串获取（阻塞）
 * 
 * @param c 
 * @return int 
 */
static int ngetc(char *c)
{
  if(_kbhit())
  {
    return getch();
  }
  return -1;
}

static int testchar(void)
{
  if(_kbhit())
  {
    return 1;
  }
  return 0;
}

/**
 * @brief 字符串输出
 * 
 * @param c 
 * @return int 
 */
static int nputc(char c)
{
  int ret = putch(c);
  if(ret == EOF)
  {
    return -1;
  }
  return c;
}

/**
 * @brief 延时
 * 
 * @param ms 
 */
static void delay(int ms)
{
	clock_t s_time = clock();
	while (clock() < s_time+ms);
}

static void usdelay(int us)
{
	clock_t s_time = clock();
	while (clock() < s_time+ us/1000);
}


static win_uart_t uart = {
    .ops.write = nputc,
    .ops.read = ngetc,
    .user.tschar = testchar,
    .user.usdelay = usdelay
};

win_uart_t* lr_get_win_uart_handle(void* obj)
{
    if(uart.ops.init != LR_NULL)
    {
        uart.ops.init();
    }
    return &uart;
}

